
#include "include.h"

u16 u16Adc_DataBufferArray[ADC_DataBufferLength]={0};

Adc_FIFOType Adc_FIFO;


static void DMA_Congfig(void)
{
	
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
	
	DMA_InitTypeDef DMA_InitStruct;
	
	DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&ADC1->DR);
	DMA_InitStruct.DMA_MemoryBaseAddr = (u32)(&u16Adc_DataBufferArray[0]);
	DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
	DMA_InitStruct.DMA_BufferSize = ADC_DataBufferLength;
  DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
	DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh;
	DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel1, &DMA_InitStruct);
	
	DMA_Cmd(DMA1_Channel1, ENABLE);
}

void Adc_Config(void)
{
	ADC_InitTypeDef ADC_InitStruct;
	
	ADC_ClockCmd();
	RCC_ADCCLKConfig(RCC_PCLK2_Div6);
	
	ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;
	ADC_InitStruct.ADC_ScanConvMode = ENABLE;
	ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
	ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
	ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
	ADC_InitStruct.ADC_NbrOfChannel = 3;
	ADC_Init(ADC1, &ADC_InitStruct);

	DMA_Congfig();
	ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_4,2,ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_5,3,ADC_SampleTime_239Cycles5);
	ADC_DMACmd(ADC1,ENABLE);
	
	ADC_Cmd(ADC1,ENABLE);
	
	ADC_ResetCalibration(ADC1);
	while(ADC_GetResetCalibrationStatus(ADC1));
	
  ADC_StartCalibration(ADC1);
  while(ADC_GetCalibrationStatus(ADC1)); 
	ADC_ExternalTrigConvCmd(ADC1, ENABLE);
}



static Bool Adc_FifoBuffer(u16 u16CurVal,u16 u16VolVal,u16 u16TempVal)
{
	Bool bRes = FALSE;
	u8 Index=0u;
	Adc_FIFOType * FifoBuffer = &Adc_FIFO;
	
	Index = FifoBuffer->u8Index;
	
	if(FifoBuffer->u8BufferCnt<ADC_BufferLength)
	{
		FifoBuffer->u8BufferCnt++;
	}
	else
	{
		FifoBuffer->u8BufferCnt = ADC_BufferLength;
		
		FifoBuffer->u32CurSampSumVal -= FifoBuffer->u16CurSampValBuffer[Index];
	  FifoBuffer->u32VolSampSumVal -= FifoBuffer->u16VolSampValBuffer[Index];
	  FifoBuffer->u32TempSampSumVal -= FifoBuffer->u16TempSampValBuffer[Index];
	}
	FifoBuffer->u16CurSampValBuffer[Index] = u16CurVal;
	FifoBuffer->u16VolSampValBuffer[Index] = u16VolVal;
	FifoBuffer->u16TempSampValBuffer[Index] = u16TempVal;
	
	FifoBuffer->u32CurSampSumVal += FifoBuffer->u16CurSampValBuffer[Index];
	FifoBuffer->u32VolSampSumVal += FifoBuffer->u16VolSampValBuffer[Index];
	FifoBuffer->u32TempSampSumVal += FifoBuffer->u16TempSampValBuffer[Index];
	
	FifoBuffer->u16CurSampAverageVal = (FifoBuffer->u32CurSampSumVal / FifoBuffer->u8BufferCnt);
	FifoBuffer->u16VolSampAverageVal = (FifoBuffer->u32VolSampSumVal / FifoBuffer->u8BufferCnt);
	FifoBuffer->u16TempSampAverageVal = (FifoBuffer->u32TempSampSumVal / FifoBuffer->u8BufferCnt);
	
	FifoBuffer->u8Index++;
	if(FifoBuffer->u8Index >= ADC_BufferLength)
	{
		FifoBuffer->u8Index = 0;
	}
	bRes = TURE;
	return bRes;
}



u16 u16Adc_CurVal=0u;
u16 u16Adc_VolVal=0u;
u16 u16Adc_TempVal=0u;
void Adc_GetSampVal(void)
{
	u8 Res = FALSE;
	u16Adc_CurVal = u16Adc_DataBufferArray[0];
	u16Adc_VolVal = u16Adc_DataBufferArray[1];
	u16Adc_TempVal = u16Adc_DataBufferArray[2];
	Res = Adc_FifoBuffer(u16Adc_CurVal,u16Adc_VolVal,u16Adc_TempVal);
	if(FALSE == Res)
	{
		return;
	}
	else
	{
	}
}




